package cn.edu.scau.cmil.linwei.comprehensive.util;

import cn.edu.scau.cmil.linwei.comprehensive.Script.*;

import java.io.*;

public class DataIO {

    public static boolean UserRead() throws IOException {//读取用户文件
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/user.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            User user;
            while((user = (User)objectin.readObject())!=null){
                DataField.userData.add(user);
                System.out.println("帐号信息："+user.ID+" "+user.PW+" "+user.Name+" "+user.UserKind+" "+user.InitQuestion+" "+user.InitAnswer);
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean UserWrite() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/user.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);
            User user;
            //user = new User("1","1","管理员Noname","0","123","123");
            //DataField.userData.add(user);

            for(int i = 0; i <DataField.userData.size();i++){
                user = DataField.userData.get(i);
                objectout.writeObject(user);
            }

            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("User写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean ScoreRead() throws IOException {//读取用户文件
            File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/score.txt");
            try{
                FileInputStream filein = new FileInputStream(file);
                ObjectInputStream objectin = new ObjectInputStream(filein);

                Score score;
                System.out.println("开始读取score");
                while((score = (Score)objectin.readObject())!=null){
                    DataField.scoreData.add(score);

                    System.out.println(score.StudentID);
                    System.out.println(score.StudentName);
                    System.out.println(score.StudentGrade);
                    System.out.println(score.StudentMajor);
                    System.out.println(score.StudentClass);
                    System.out.println(score.TeacherName);
                    System.out.println(score.CourseName);
                    System.out.println(score.Attend);
                    System.out.println(score.Exam);
                    System.out.println(score.Work);
                    System.out.println(score.FinalExam);
                    System.out.println(score.TotalScore);

                }
                objectin.close();
                filein.close();
                System.out.println("score读取完毕");
            }
            catch (ClassNotFoundException e){
                System.out.println("不能读出对象"+e);
                return false;
            }
            return true;

    }

    public static boolean ScoreWrite() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/score.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);
            Score score = new Score();
            //System.out.println("写入的信息为");
            for(int i = 0; i <DataField.scoreData.size();i++){
                score = DataField.scoreData.get(i);

                /*
                System.out.println(score.StudentID);
                System.out.println(score.StudentName);
                System.out.println(score.StudentGrade);
                System.out.println(score.StudentMajor);
                System.out.println(score.StudentClass);
                System.out.println(score.TeacherName);
                System.out.println(score.CourseName);
                System.out.println(score.Attend);
                System.out.println(score.Exam);
                System.out.println(score.Work);
                System.out.println(score.FinalExam);
                System.out.println(score.TotalScore);
                */
                objectout.writeObject(score);
            }

            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("Score写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean TeacherRead() throws IOException {//读取用户文件
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/teacher.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            Teacher teacher;
            System.out.println("开始读入教师信息");
            while((teacher = (Teacher) objectin.readObject())!=null){
                DataField.teacherData.add(teacher);
            }
            System.out.println("教师信息读入完成");

            for(int i = 0;i<DataField.teacherData.size();i++){//遍历教师数据
                System.out.println(DataField.teacherData.get(i).ID);//获取第I个教师的ID
                System.out.println(DataField.teacherData.get(i).Name);//获取第I个教师的姓名
                for(String tmp:DataField.teacherData.get(i).teachCourse){//获取第I个教师的教学科目
                    System.out.println("课程："+tmp);
                }
            }

            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;

    }

    public static boolean TeacherWrite() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/teacher.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            Teacher teacher;
            /*
            ArrayList<String> teachCourse = new ArrayList<>();
            teachCourse.add("面向对象程序设计");teachCourse.add("C语言程序设计");
            DataField.teacherData.add(new Teacher("10086","肖磊",teachCourse));
            */
            for(int i = 0; i <DataField.teacherData.size();i++){
                teacher = DataField.teacherData.get(i);
                objectout.writeObject(teacher);
            }
            /*
            ArrayList<String> teachCourse = new ArrayList<>();
            teachCourse.add("面向对象程序设计");teachCourse.add("C语言程序设计");
            objectout.writeObject(new Teacher("10086",teachCourse));//写入
            */
            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
           // System.out.println("写入完成");
        }
        catch (IOException e){
            System.out.println("Teacher写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean StudentRead() throws IOException {//读取用户文件
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/student.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            Student student;
            System.out.println("开始读入学生数据");
            while((student = (Student) objectin.readObject())!=null){
                DataField.studentData.add(student);
            }
            System.out.println("学生数据读入完成");

            for(int i = 0;i<DataField.studentData.size();i++){//遍历学生数据
                System.out.println(DataField.studentData.get(i).ID);//获取第I个学生的ID
                System.out.println(DataField.studentData.get(i).Name);//获取第I个学生的ID
                StudentClass tmp = DataField.studentData.get(i).myclass;
                System.out.println(tmp.StudentGrade+"级"+tmp.StudentMajor+"专业"+tmp.StudentClass+"班");
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean StudentWrite() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/student.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);
            Student student;
            /*
            StudentClass sc = new StudentClass("2017","软件工程","3");
            Student s = new Student("201727010301","17软工三班1号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010302","17软工三班2号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010303","17软工三班3号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010304","17软工三班4号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010305","17软工三班5号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010306","17软工三班6号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010307","17软工三班7号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010308","17软工三班8号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010309","17软工三班9号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010310","17软工三班10号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010311","17软工三班11号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010312","17软工三班12号",sc);
            DataField.studentData.add(s);
            s = new Student("201727010313","17软工三班13号",sc);
            DataField.studentData.add(s);
            */

            for(int i = 0; i <DataField.studentData.size();i++){
                student = DataField.studentData.get(i);
                objectout.writeObject(student);
            }


            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
            // System.out.println("写入完成");
        }
        catch (IOException e){
            System.out.println("Teacher写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean WhiteListRead() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/whitelist.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            String tmp;
            while((tmp = (String)objectin.readObject())!=null){
                DataField.whiteList.add(tmp);
                System.out.println("白名单："+tmp);
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean WhiteListWrite() throws IOException{  //未完成
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/whitelist.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            String tmp;
            /*
            DataField.whiteList.add("201727010301");
            DataField.whiteList.add("201727010302");
            DataField.whiteList.add("201727010303");
            DataField.whiteList.add("201727010304");
            DataField.whiteList.add("201727010305");
            DataField.whiteList.add("201727010306");
            DataField.whiteList.add("201727010307");
            DataField.whiteList.add("201727010308");
            DataField.whiteList.add("201727010309");
            DataField.whiteList.add("201727010310");
            DataField.whiteList.add("201727010311");
            DataField.whiteList.add("201727010312");
            DataField.whiteList.add("10086");
            DataField.whiteList.add("1");
            */

            for(int i = 0; i <DataField.whiteList.size();i++){
                tmp = DataField.whiteList.get(i);
                objectout.writeObject(tmp);
            }

            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("WhiteList写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean GradeRead() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/grade.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            String tmp;
            while((tmp = (String)objectin.readObject())!=null){
                DataField.gradeList.add(tmp);
                System.out.println("年级："+tmp);
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean GradeWrite() throws IOException{  //未完成
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/grade.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            String tmp;
            //DataField.gradeList.add("2017");
            for(int i = 0; i <DataField.gradeList.size();i++){
                tmp = DataField.gradeList.get(i);
                objectout.writeObject(tmp);
            }
            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("gradeList写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean MajorRead() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/major.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            String tmp;
            while((tmp = (String)objectin.readObject())!=null){
                DataField.majorList.add(tmp);
                System.out.println("专业："+tmp);
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean MajorWrite() throws IOException{  //未完成
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/major.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            String tmp;
            //DataField.majorList.add("软件工程");
            //DataField.majorList.add("应用数学");
            for(int i = 0; i <DataField.majorList.size();i++){
                tmp = DataField.majorList.get(i);
                objectout.writeObject(tmp);
            }
            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("majorList写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean CourseRead() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/course.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            String tmp;
            while((tmp = (String)objectin.readObject())!=null){
                DataField.courseList.add(tmp);
                System.out.println("课程："+tmp);
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean CourseWrite() throws IOException{  //未完成
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/course.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            String tmp;
            //DataField.courseList.add("面向对象程序设计");
            //DataField.courseList.add("C语言程序设计");
            for(int i = 0; i <DataField.courseList.size();i++){
                tmp = DataField.courseList.get(i);
                objectout.writeObject(tmp);
            }
            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("courseList写入失败"+e);
            return false;
        }
        return true;
    }

    public static boolean ClassRead() throws IOException{
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/class.txt");
        try{
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream objectin = new ObjectInputStream(filein);
            StudentClass tmp;
            while((tmp = (StudentClass) objectin.readObject())!=null){
                DataField.classList.add(tmp);
                System.out.println(tmp.StudentGrade+"级"+tmp.StudentMajor+"专业"+tmp.StudentClass+"班");
            }
            objectin.close();
            filein.close();
        }
        catch (ClassNotFoundException e){
            System.out.println("不能读出对象"+e);
            return false;
        }
        return true;
    }

    public static boolean ClassWrite() throws IOException{  //未完成
        File file = new File("src/cn/edu/scau/cmil/linwei/comprehensive/Data/class.txt");
        try{
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream objectout = new ObjectOutputStream(fileout);

            StudentClass tmp;
/*
            DataField.classList.add(new StudentClass("2017","软件工程","1"));
            DataField.classList.add(new StudentClass("2017","软件工程","2"));
            DataField.classList.add(new StudentClass("2017","软件工程","3"));
            DataField.classList.add(new StudentClass("2017","应用数学","1"));
            DataField.classList.add(new StudentClass("2017","应用数学","2"));
            DataField.classList.add(new StudentClass("2017","应用数学","3"));
*/
            for(int i = 0; i <DataField.classList.size();i++){
                tmp = DataField.classList.get(i);
                objectout.writeObject(tmp);
            }
            objectout.writeObject(null);//最后写一个null作为结束标记，才能不出错
            objectout.close();
            fileout.close();
        }
        catch (IOException e){
            System.out.println("classList写入失败"+e);
            return false;
        }
        return true;
    }

}
